/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package web;

import entities.User;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;

/**
 *
 * @author Alejandro
 */
@ManagedBean
@RequestScoped
public class ControllerBean {

    private boolean loged;

    public ControllerBean() {
    }

    public Boolean checkCategory(String view) {
        Boolean res = false;
        if (view != null && !view.isEmpty()) {
            String[] arr = view.split(":");
            res = arr[0].equals(Constants.CATEGORY);
        }
        return res;
    }

    public Boolean checkItem(String view) {
        Boolean res = false;
        if (view != null && !view.isEmpty()) {
            String[] arr = view.split(":");
            res = arr[0].equals(Constants.ITEM);
        }
        return res;
    }

    public Boolean checkCart(String view) {
        Boolean res = false;
        if (view != null && !view.isEmpty()) {
            String[] arr = view.split(":");
            res = arr[0].equals(Constants.CART);
        }
        return res;
    }

    public Boolean checkAccount(String view) {
        Boolean res = false;
        if (view != null && !view.isEmpty()) {
            String[] arr = view.split(":");
            res = arr[0].equals(Constants.ACCOUNT);
        }
        return res;
    }

    public Boolean checkSearch(String view) {
        Boolean res = false;
        if (view != null && !view.isEmpty()) {
            String[] arr = view.split(":");
            res = arr[0].equals(Constants.SEARCH);
        }
        return res;
    }

    public String debugSetItemView() {
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("nextView", Constants.ITEM + Constants.SEPARATOR + 10);
        return "UPDATE";
    }

    public Boolean userLoged() {
        User user = (User) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get(Constants.ACTUAL_USER);
        return user != null;
    }

    public Boolean adminLoged() {
        User user = (User) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get(Constants.ACTUAL_USER);
        return user != null && user.getPermission().equals(2);
    }

    public boolean isLoged() {
        loged = userLoged();
        return loged;
    }

    public String logout() {
        FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
        return "LOGOUT";
    }

    public String myAccount() {
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put(Constants.NEXT_VIEW, Constants.ACCOUNT + Constants.SEPARATOR + 1);
        return "UPDATE";
    }

    public String backoffice() {//Cualquier vista no soportada por el backoffice para resetear la navegacion
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put(Constants.BC_NEXT_VIEW, Constants.ACCOUNT + Constants.SEPARATOR + 1);

        return "BACKOFFICE";
    }

    public String search() {
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put(Constants.NEXT_VIEW, Constants.SEARCH + Constants.SEPARATOR + 1);

        return "UPDATE";
    }

    public String home() {
             
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put(Constants.NEXT_VIEW, null);
        return "UPDATE";
    }
}
